% grid: 10 options for gamma 13 options for beta

conv_rate = zeros(29,13); % rate of convergence
conv_step = zeros(29,13); % average step of convergence (if converged), % 0 if all didnt converge
glbl_optm = zeros(29,13); % rate of finding global optima (out of N)

N = 1024;

currentFolder = fileparts(mfilename('fullpath')); % Get script folder
files = dir(fullfile(currentFolder, '*.mat')); % Get all .mat files in the folder

for k = 1:length(files)
    fileName = fullfile(currentFolder, files(k).name); % Get full file path
    data = load(fileName); % Load the .mat file
    disp(['Loaded: ', files(k).name]); % Display the file name

    % Process data here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if size(data.data)*[0;1] ~= N
        % disp(['Loaded: ', files(k).name]); % Display the file name
        error("incomplete dataset at file:")
    end

    % matrix(i,j)
    gamma = data.gamma;
    beta = data.beta;
    if gamma < 0.991
        i = round((gamma-0.8)*100+1);
    else
        i = round((gamma-0.99)*1000+20);
    end
    j = round(beta*20+1);

    conv_count = 0;
    conv_step_total = 0;
    glbl_optm_count = 0;

    for l = 1:N
        if data.data(l).id ~= 0
            conv_count = conv_count + 1;
            conv_step_total = conv_step_total + data.data(l).k_end;
        end
        if data.data(l).id == 4
            glbl_optm_count = glbl_optm_count + 1;
        end
        
        if conv_count == 0
            conv_rate(i,j) = 0;
            conv_step(i,j) = 0;
            glbl_optm(i,j) = 0;
        else
            conv_rate(i,j) = conv_count/N;
            conv_step(i,j) = conv_step_total/conv_count;
            glbl_optm(i,j) = glbl_optm_count/N;
        end

    end

end